<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\HomeUserModel;
use App\Http\Requests\Home\CreateOrderAddress;
use App\Model\HomeUserAddress;
use DB;

class UserAddressController extends Controller
{
    // 创建收货地址
    public function CreateAddress(CreateOrderAddress $request)
    {
        // 准备参数
        $province = $request->province;
        $city = $request->city;
        $area = $request->area;

        // 判断是否合法
        if ($province == '请选择' || $city == '请选择' || $area == '请选择') {
            return 500;
        }

        // 获取session信息
        $session = $request->session()->get('Homeuser-data');

        $data = HomeUserAddress::where('user_id', $session['id'])->get()->toArray();

        // 是默认值唯一
        $default = 1;

        // 判断是否是第一次添加地址
        if (isset($data[0])) {
            // 不是默认值为2
            $default = 2;
        }
        // 统计数量
        $number = count($data);
        // 判断是否多于三条
        if ($number > 2) {
            return 403;
            dd($number);
        }

        // 准备省市区
        $ssq = $province.$city.$area;

        // 添加数据
        if ($default == 1) {
            $id = DB::table('shop_user_addresses')->insertGetId([
                'user_id'           => $session['id'],
                'getname'           => $request->getname,
                'phone'             => $request->phone,
                'address'           => $request->addressdetail,
                'whether_address'   => $default,
                'ssq'               => $ssq,
            ]);

            // 返回成功
            return ['status' => 200, 'id' => $id];
        }


        $id = DB::table('shop_user_addresses')->insertGetId([
            'user_id'           => $session['id'],
            'getname'           => $request->getname,
            'phone'             => $request->phone,
            'address'           => $request->addressdetail,
            'whether_address'   => $default,
            'ssq'               => $ssq,
        ]);

        // 返回成功
        return ['status' => 2200, 'id' => $id];

    }

    // 编辑收货地址
    public function UpdateAddress(CreateOrderAddress $request)
    {
        // 准备参数
        $province = $request->province;
        $city = $request->city;
        $area = $request->area;

        // 判断是否合法
        if ($province == '请选择' || $city == '请选择' || $area == '请选择') {
            return 500;
        }

        // 获取session信息
        $session = $request->session()->get('Homeuser-data');

        // 准备省市区
        $ssq = $province.$city.$area;

        // 得到该地址的信息
        $addredss = HomeUserAddress::where('id', $request->id)->first();

        // 更新数据
        $result = HomeUserAddress::where('id', $request->id)
        ->update([
          'user_id'         => $session['id'],
          'getname'         => $request->getname,
          'phone'           => $request->phone,
          'address'         => $request->addressdetail,
          'whether_address' => $addredss->whether_address,
          'ssq'             => $ssq,
        ]);

        // 返回成功
        return 200;
    }

    // 删除地址
    public function DeStroy(Request $request)
    {
        HomeUserAddress::where('id', $request->id)->delete();
    }

    // 更改默认地址
    public function ChangeDefaultAddress(Request $request)
    {
        // 得到用户信息
        $user = $request->session()->get('Homeuser-data');

        // 更改默认地址为非默认地址
        $defaultaddress = HomeUserAddress::where([['user_id', '=', $user['id']], ['whether_address', '=', 1]])->get()->toArray();

        // 循环更改
        foreach ($defaultaddress as $v) {
            HomeUserAddress::where('id', $v['id'])
                ->update([
                    'whether_address' => 2,
                ]);
        }

        // 将用户要更改的地址改为默认地址
        $result = HomeUserAddress::where('id', $request->id)
            ->update([
                'whether_address' => 1,
            ]);
    }

}
